<template>
	<view>
		<!-- 搜索组件 -->
		<my-search bgcolor="#f0f" radius="9" @click="gotoSearch"></my-search>

		<!-- 分类导航 -->
		<view class="cate-container">
			<!-- 左侧一级导航 -->
			<scroll-view class="cate-left" scroll-y :style="{height: wh+'px'}">
				<!-- <view :class="['cate-left-item', i === active ? 'active' : '']" v-for="(item, i) in cateList" :key="i"> -->
				<view :class="{'cate-left-item': true, 'active': i === active }" v-for="(item, i) in cateList" :key="i"
					@click="changeActive(i)">
					{{item.cat_name}}
				</view>
			</scroll-view>


			<!-- 右侧二级分类 -->
			<scroll-view class="cate-right" scroll-y :style="{height: wh+'px'}" :scroll-top="scrollTop">

				<view class="cate-right-item" v-for="(item, i) in cateLevel2" :key="i">
					<!-- 二级分类title -->
					<view class="cate-right-item-title">
						/ {{item.cat_name}} /
					</view>
					<!-- 二级分类内容 -->
					<view class="cate-right-item-content">

						<view class="cate-right-item-content-box" v-for="(item2, i2) in item.children" :key="i2"
							@click="gotoGoodsList(item2)">
							<!-- 二级分类图片 -->
							<image :src="item2.cat_icon" mode="widthFix"></image>
							<!-- 二级分类名称 -->
							<text>{{item2.cat_name}}</text>
						</view>

					</view>
				</view>

			</scroll-view>

		</view>
	</view>
</template>

<script>
	import badgeMix from '@/mixins/tabbar-badge.js'
	export default {
		mixins: [badgeMix],
		data() {
			return {
				// 分类数据列表
				cateList: [],
				// 二级分类数据
				cateLevel2: [],

				// 页面状态数据
				wh: 0, // 屏幕可用高度
				active: 0, // 选中的一级分类索引
				// 滚动条距离顶部的距离
				scrollTop: 0
			};
		},
		onLoad() {
			// 发起网络请求
			this.getCateList()

			// 获取设备信息
			const sysInfo = uni.getSystemInfoSync()
			this.wh = sysInfo.windowHeight
		},
		methods: {
			// 获取分类数据
			async getCateList() {
				const {
					data: res
				} = await uni.$http.get('/categories')
				// 数据异常判断
				if (res.meta.status !== 200) return uni.$showMsg()

				// 设置页面数据
				this.cateList = res.message
				// 设置二级分类数据
				this.cateLevel2 = res.message[this.active].children
			},

			// 一级分类点击事件处理函数
			changeActive(i) {
				console.log(i)
				this.active = i
				// 设置二级分类数据
				this.cateLevel2 = this.cateList[this.active].children
				// 设置二级分类滚动条的位置
				this.scrollTop = Math.random() // 数据变化驱动页面刷新
			},

			// 点击二级分类跳转
			gotoGoodsList(goods) {
				console.log(goods)
				uni.navigateTo({
					url: `/subpkg/goods_list/goods_list?cid=${goods.cat_id}`
				})
			},
			// 点击搜索组件跳转到搜索页面
			gotoSearch(args) {
				console.log('父组件事件监听', args)
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			}
		}
	}
</script>

<style lang="scss">
	.cate-container {
		display: flex;

		.cate-left {
			width: 240rpx;

			.cate-left-item {
				display: flex;
				justify-content: center;
				align-items: center;
				height: 120rpx;
				background-color: #efefef;
				font-size: 24rpx;

				&.active {
					background-color: #fff;
					display: flex;
					justify-content: space-between;

					&::before {
						content: '';
						width: 6rpx;
						height: 60rpx;
						background-color: #c00000;
					}

					&::after {
						content: '';
						width: 6rpx;
						height: 60rpx;
						background-color: #fff;
					}
				}

			}
		}

		.cate-right {
			background-color: white;

			.cate-right-item {

				.cate-right-item-title {
					display: flex;
					justify-content: center;
					font-weight: bold;
					font-size: 24rpx;
					padding: 20rpx 0;
				}

				.cate-right-item-content {
					display: flex;
					flex-wrap: wrap;

					.cate-right-item-content-box {
						width: 33.33%;
						display: flex;
						flex-direction: column;
						justify-content: center;
						align-items: center;
						padding: 20rpx;
						box-sizing: border-box;

						image {
							width: 100%;
						}

						text {
							font-size: 24rpx;
						}
					}
				}

			}
		}
	}
</style>
